package global

import (
	"fmt"
	_const "longmen/server/pkg/common/helper/const"

	cfg "github.com/ufoscout/go-up"
)

var Config cfg.GoUp

const BatchSize = 1000

func InitConf(filepath string) error {
	fName := "InitConf"

	config, err := cfg.NewGoUp().AddFile(filepath, false).Build()
	if err != nil {
		return fmt.Errorf("%s fail %s , file path is ； %s", fName, err, filepath)
	}
	_const.StreamCallBackKey = config.GetString("stream.callback_key")
	_const.SwagIsEnable = config.GetBool("swagger.is.enable")
	Config = config
	return nil
}

/**
 * @Author admin
 * @Description //获取微服务名称
 * @version: v1.8.0
 * @Date 13:26 2022/8/12
 * @Param
 * @return
 **/
func GetMicroserviceName(key string) string {
	serverName := Config.GetString(key)
	if len(serverName) <= 0 {
		switch key {
		case _const.GRPC_ACCOUNT_SERVER_NAME_KEY:
			serverName = _const.GRPC_ACCOUNT_SERVER_NAME
		case _const.GRPC_LIVE_SERVER_NAME_KEY:
			serverName = _const.GRPC_LIVE_SERVER_NAME
		case _const.GRPC_SPORT_SERVER_NAME_KEY:
			serverName = _const.GRPC_SPORT_SERVER_NAME
		case _const.GRPC_TASK_SERVER_NAME_KEY:
			serverName = _const.GRPC_TASK_SERVER_NAME
		case _const.GRPC_HONG_BAO_SERVER_NAME_KEY:
			serverName = _const.GRPC_HONG_BAO_SERVER_NAME
		case _const.GRPC_GUESS_SERVER_NAME_KEY:
			serverName = _const.GRPC_GUESS_SERVER_NAME
		case _const.GRPC_WS_SERVER_NAME_KEY:
			serverName = _const.GRPC_WS_SERVER_NAME
		case _const.GRPC_PAYMENT_SERVER_NAME_KEY:
			serverName = _const.GRPC_PAYMENT_SERVER_NAME
		case _const.GRPC_TENCENT_IM_SERVER_NAME_KEY:
			serverName = _const.GRPC_TENCENT_IM_SERVER_NAME
		case _const.GRPC_GAMES_SERVER_NAME_KEY:
			serverName = _const.GRPC_GAMES_SERVER_NAME
		}
	}
	return serverName
}
func GetMicroserviceVersion() string {
	version := Config.GetString("grpc.microservice.version")
	if len(version) <= 0 {
		version = _const.GRPC_MICROSERVICE_VERSION
	}
	return version
}

/**
 * @Author admin
 * @Description //获取IMAPP ID
 * @version: v1.8.0
 * @Date 14:54 2022/8/17
 * @Param
 * @return
 **/
func GetTenCentAppId() int {
	appId := Config.GetInt("tencent.im.sdk.app.id")
	if appId <= 0 {
		appId = _const.TENCENT_IM_SDK_APP_ID
	}
	return appId
}

/**
 * @Author admin
 * @Description //im账号登陆过期时间
 * @version: v1.8.0
 * @Date 14:54 2022/8/17
 * @Param
 * @return
 **/
func GetTenCentAccExpireTime() int {
	appId := Config.GetInt("tencent.im.account.expire.timehelper")
	if appId <= 0 {
		appId = _const.TENCENT_IM_ACCOUNT_EXPIRE_TIME
	}
	return appId
}

/**
 * @Author admin
 * @Description //虚构账号存在时间
 * @version: v1.8.0
 * @Date 9:36 2022/8/18
 * @Param
 * @return
 **/
func GetTenCentUnrealAccExpireTime() int {
	appId := Config.GetInt("tencent.im.Unreal.account.expire.timehelper")
	if appId <= 0 {
		appId = _const.TENCENT_IM_UNREALACCOUNT_EXPIRE_TIME
	}
	return appId
}

/**
 * @Author admin
 * @Description //IM 密钥
 * @version: v1.8.0
 * @Date 14:54 2022/8/17
 * @Param
 * @return
 **/
func GetTenCentSecretKey() string {
	secretKey := Config.GetString("tencent.im.secret.key")
	if len(secretKey) <= 0 {
		secretKey = _const.TENCENT_IM_SECRET_KEY
	}
	return secretKey
}
